package top.frsf.pattern.a_creational.c_builder.a_static;

/**
 * 建造者模式 HTTP 客户端
 *
 * @author He Kuntao
 * @since 2025/6/16 11:02
 */
public class HttpClient {
    // 必选参数
    private final String baseUrl;
    // 可选参数（带默认值）
    private final Long ConnectTimeout;
    private final int maxRetries;

    //---------------- Builder核心实现 -------------------
    public static Builder builder() {
        return new Builder();
    }

    public HttpClient(Builder builder) {
        this.baseUrl = builder.baseUrl;
        this.ConnectTimeout = builder.ConnectTimeout;
        this.maxRetries = builder.maxRetries;
    }

    @Override
    public String toString() {
        return "HttpClient{" +
                "baseUrl='" + baseUrl + '\'' +
                ", ConnectTimeout=" + ConnectTimeout +
                ", maxRetries=" + maxRetries +
                '}';
    }

    //---------------- Builder静态内部类 -------------------
    public static class Builder {
        // 必选参数
        private String baseUrl;
        // 可选参数（带默认值）
        private Long ConnectTimeout = 1000L;
        private int maxRetries = 3;


        // 必选参数设置
        public Builder baseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
            return this;
        }

        public Builder ConnectTimeout(Long ConnectTimeout) {
            this.ConnectTimeout = ConnectTimeout;
            return this;
        }

        public Builder maxRetries(int maxRetries) {
            this.maxRetries = maxRetries;
            return this;
        }

        // 构建方法（校验必选参数）
        public HttpClient build() {
            if (baseUrl == null || baseUrl.isEmpty()) {
                throw new IllegalStateException("baseUrl不能为空");
            }
            return new HttpClient(this);
        }
    }
}
